#include "junix.h"


struct Item
{
	Item(std::string s="", int c=0)
		:word(s),count(c)
	{ }

	std::string word;
	int count;
};

int main()
{
	std::ifstream file("BIGTXT");
	if (!file) {
		std::cerr<<"file open error"<<std::endl;
		exit(EXIT_FAILURE);
	}

	std::string s;
	typedef std::map<std::string, int> WMAP;
	WMAP words;
	while (file>>s) {
		++words[s];
	}

	jtime T;
	T.start();
	typedef std::vector<Item> VITEM;
	VITEM V;
	V.reserve(words.size());
	BOOST_FOREACH (WMAP::reference v, words) {
		V.push_back(Item(v.first,v.second));
	}

	words.clear();

	using boost::bind;
	using std::greater;

	partial_sort(V.begin(),V.begin()+10,V.end(),
			bind(greater<int>(),
				bind(&Item::count, _1),
				bind(&Item::count, _2)));
	T.end();
	T.print();


	V.erase(V.begin()+10,V.end());

	VITEM::iterator pos;
	BOOST_FOREACH(Item& v, V) {
		std::cout<<"("<<v.word<<","<<v.count<<")"<<std::endl;
	}
}
